﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cnFederal
{
     
    public class CnfiUsers
    {
        private static CnfiDataDataContext lnq;
        private static List<CnfiUser> _userList;

        public CnfiUsers()
        {

        }

        public List<CnfiUser> UserList
        {
            get
            {
                return _userList;
            }
            set
            {
                _userList = value;
            }
        }

        public static void Load()
        {
            lnq = new CnfiDataDataContext();
            _userList = new List<CnfiUser>();
            if (_userList != null)
            {
                List<CnfiUser> liste = new List<CnfiUser>();
                var b = from c in lnq.CnfUsers
                        select c;
                foreach (var s in b)
                {
                    CnfiUser usr = new CnfiUser();
                    usr.Id = (int)s.Id;
                    usr.Ipadresse = (string)s.Ipadresse;
                    if(s.IsAdmin != null)
                    usr.IsAdmin = (bool)s.IsAdmin;
                    if (s.IsAutor != null)
                    usr.IsAuthor = (bool)s.IsAutor;
                    usr.Email = (string)s.Email;
                    usr.MacId = (string)s.MacId;
                    usr.Mobile = (string)s.Mobile;
                    usr.Name = (string)s.Name;
                    usr.Password = (string)s.Password;
                    usr.Tlf = (string)s.Tlf;
                    usr.Username = (string)s.Username;
                    if(s.DateRegistered != null)
                    usr.DateRegistered = (DateTime)s.DateRegistered;
                    liste.Add(usr);

                }
                _userList = liste;
            }
        }

        public void RefreshUserList()
        {
            _userList.Clear();
            _userList = GetAllUsers();
        }

        public static List<CnfiUser> GetAllUsers()
        {

            return _userList;
        }

        public static CnfiUser GetUserByEmail(string mail)
        {
            if (string.IsNullOrWhiteSpace(mail)) return null;
            CnfiUser usr = null;
            if (_userList == null) return null;
            foreach (CnfiUser u in _userList)
            {
                if (!string.IsNullOrWhiteSpace(u.Email))
                    if (u.Email.ToLower().Trim() == mail.ToLower().Trim())
                        usr = u;
            }
            return usr;
        }
        public static CnfiUser GetUserById(int id)
        {
            if (_userList == null) return null;
            CnfiUser usr = null;
            var b = from c in _userList
                    where c.Id == id
                    select c;
            foreach (var s in b)
            {

                usr = new CnfiUser();
                usr.Id = (int)s.Id;
                if (s.Email != null)
                usr.Email = (string)s.Email;
                if (s.Ipadresse != null)
                usr.Ipadresse = (string)s.Ipadresse;
                if (s.IsAuthor != null)
                usr.IsAuthor = (bool)s.IsAuthor;
                if (s.IsAdmin != null)
                usr.IsAdmin = (bool)s.IsAdmin;
                if (s.MacId != null)
                usr.MacId = (string)s.MacId;
                if (s.Mobile != null)
                usr.Mobile = (string)s.Mobile;
                if (s.Name != null)
                usr.Name = (string)s.Name;
                if (s.Password != null)
                usr.Password = (string)s.Password;
                if (s.Tlf != null)
                usr.Tlf = (string)s.Tlf;
                if (s.Username != null)
                usr.Username = (string)s.Username;
                if (s.DateRegistered != null)
                usr.DateRegistered = (DateTime)s.DateRegistered;
                
            }
            return usr;

        }

        public static bool CreateNew(CnfiUser c)
        {
            bool created = false;
            try
            {
                CnfUser usr = new CnfUser();
                usr.Email = c.Email;
                usr.Ipadresse = c.Ipadresse;
                usr.IsAdmin = c.IsAdmin;
                usr.IsAutor = c.IsAuthor;
                usr.MacId = c.MacId;
                usr.Mobile = c.Mobile;
                usr.Name = c.Name;
                usr.Password = c.Password;
                usr.Tlf = c.Tlf;
                usr.Username = c.Username;
                usr.DateRegistered = c.DateRegistered;
                lnq.CnfUsers.InsertOnSubmit(usr);
                lnq.SubmitChanges();
                lnq.Connection.Close();
                created = true;
            }
            catch (Exception ex)
            {
                created = false;

            }
            return created;

        }

        public static bool DeleteItem(int id)
        {
            bool deleted = false;
            var b = from f in lnq.CnfUsers
                    where f.Id == id
                    select f;
            if (b != null && b.Count() > 0)
            {
                CnfUser n = b.First();
                try
                {
                    lnq.CnfUsers.DeleteOnSubmit(n);
                    lnq.SubmitChanges();
                    deleted = true;
                }
                catch (Exception ex)
                {
                    deleted = false;
                }
            }
            return deleted;
        }
        public static void Save(CnfiUser usr)
        {
            var b = from c in lnq.CnfUsers
                    where c.Id == usr.Id
                    select c;
            foreach (var u in b)
            {
                u.Email = usr.Email;
                u.Id = usr.Id;
                u.Ipadresse = usr.Ipadresse;
                u.IsAdmin = usr.IsAdmin;
                u.IsAutor = usr.IsAuthor;
                u.MacId = usr.MacId;
                u.Mobile = usr.Mobile;
                u.Name = usr.Name;
                u.Password = usr.Password;
                u.Tlf = usr.Tlf;
                u.Username = usr.Username;
                u.DateRegistered = usr.DateRegistered;
            }
            lnq.SubmitChanges();

        }

      

    }



    public class CnfiUser
    {

        public int Id
        {
            get;
            set;
        }
        public string Username
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string Email
        {
            get;
            set;
        }
        public string Tlf
        {
            get;
            set;
        }
        public string Mobile
        {
            get;
            set;
        }
        public bool IsAdmin
        {
            get;
            set;
        }
        public bool IsAuthor
        {
            get;
            set;
        }
        public string MacId
        {
            get;
            set;
        }
        public string Password
        {
            get;
            set;
        }
        public string Ipadresse
        {
            get;
            set;
        }
        public DateTime DateRegistered
        {
            get;
            set;
        }

    }
}